package com.knowledge.knowledge_notes.controller;

import com.knowledge.knowledge_notes.annotation.ApiRestController;
import com.knowledge.knowledge_notes.common.ResultVo;
import com.knowledge.knowledge_notes.pojo.AdminMenu;
import com.knowledge.knowledge_notes.pojo.LoginUser;
import com.knowledge.knowledge_notes.service.AdminMenuService;
import com.knowledge.knowledge_notes.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Menu controller.
 *
 * @author Evan
 * @date 2019/11
 */
@ApiRestController
public class MenuController {
    @Autowired
    AdminMenuService adminMenuService;
    @Autowired
    private UserService userService;

    @GetMapping("/menu")
    public ResultVo menu() {
        return ResultVo.OK(adminMenuService.getMenusByCurrentUser());
    }

    @GetMapping("/admin/role/menu")
    public ResultVo listAllMenus() {
        String userName = (String) SecurityUtils.getSubject().getPrincipal();
        LoginUser user = userService.findByUserName(userName);
        List<AdminMenu> adminMenuList = new ArrayList<>();
        user.getRoles().stream().forEach((e) -> {
            adminMenuList.removeAll(adminMenuService.getMenusByRoleId(e.getId()));
            adminMenuList.addAll(adminMenuService.getMenusByRoleId(e.getId()));
        });
        return ResultVo.OK(adminMenuList);
//        return ResultVo.OK(adminMenuService.getMenusByRoleId(1));
    }
}
